﻿
using System.Collections.Generic;
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace mamnontuoitho7.AppCode
{
    public class ConnectGetData
    {
            public ConnectGetData()
            {
                //
                // TODO: Add constructor logic here
                //
            }

            /// <summary>
            /// Get Connection
            /// </summary>
            /// <returns></returns>
            public SqlConnection GetConnect()
            {
                SqlConnection connect = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
                
                return connect;
            }

            /// <summary>
            /// Hàm trả về 1 dataset từ câu truy vấn sql
            /// </summary>
            /// <param name="sqlDS">Input string sql</param>
            /// <returns>Data Set</returns>
            public DataSet GetDataSet(string sqlDS)
            {
                SqlConnection conDS = GetConnect();
                SqlDataAdapter daDS = new SqlDataAdapter(sqlDS, conDS);
                DataSet ds = new DataSet();
                daDS.Fill(ds);
                return ds;
            }


            /// <summary>
            /// Hàm trả về datatable từ câu truy vấn
            /// </summary>
            /// <param name="sqlDT">Input string sql</param>
            /// <returns>Data Table</returns>
            public DataTable GetDataTable(string sqlDT)
            {
                SqlConnection conDT = GetConnect();
                SqlDataAdapter daDT = new SqlDataAdapter(sqlDT, conDT);
                DataTable dt = new DataTable();
                daDT.Fill(dt);
                return dt;
            }

            ///  <summary>
            ///  Get datatable by parameters
            ///  </summary>
            ///  <param name="cmd"></param>
            ///  <returns></returns>
            internal DataTable GetDataTable(SqlCommand cmd)
            {
                SqlDataAdapter daDT = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                daDT.Fill(dt);
                return dt;
            }

    
            /// <summary>
            /// Hàm thực thi câu lệnh sql không trả về kết quả, dùng để Insert, Update, Delete
            /// </summary>
            /// <param name="sqlENQ">Input string sql</param>
            public void ExcuteNonQuery(string sqlENQ)
            {
                SqlConnection conENQ = GetConnect();
                conENQ.Open();
                SqlCommand cmdENQ = new SqlCommand(sqlENQ, conENQ);
                cmdENQ.ExecuteNonQuery();
                cmdENQ.Dispose(); //Hủy sqlcommand
                conENQ.Close();
            }
        }

}